package com.vogue.supplymall.admin.business.controller;

import com.vogue.supplymall.admin.business.domain.biz.OpenShopBiz;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.admin.LicenceSearchInfo;
import com.vogue.supplymall.common.domain.dto.admin.ShopLicenseCheckInfo;
import com.vogue.supplymall.common.domain.dto.admin.ShopSearchInfo;
import com.vogue.supplymall.common.exception.ParamErrorException;
import com.vogue.supplymall.cross.domain.service.AdminManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * The type Open shop controller.
 * 开通店铺的controller
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-04-27
 */
@Validated
@RestController
public class OpenShopController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger LOGGER_OPENSHOPCONTROLLER = LoggerFactory.getLogger(OpenShopController.class);

    @Resource
    private OpenShopBiz openShopBiz;
    @Resource
    private AdminManagementService adminManagementService;

    /**
     * Gets shop statistics.
     * 获取店铺统计数
     *
     * @param appver    the appver
     * @param syscodeid the syscodeid
     * @return the shop statistics
     */
    @RequestMapping(value = "/is/{appver}/admin/business/shop/{syscodeid}/statistics", method = RequestMethod.POST)
    public ResponseEntity<Object> getShopStatistics(@PathVariable("appver") String appver, @PathVariable("syscodeid") Long syscodeid) {
        LOGGER_OPENSHOPCONTROLLER.info("get Shop Statistics start >>>>>> ");
        if (null == syscodeid || 0L == syscodeid) {
            throw new ParamErrorException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(openShopBiz.getShopStatistics(syscodeid));
        LOGGER_OPENSHOPCONTROLLER.info(" <<<<<< get Shop Statistics end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Gets open organ list.
     * 查询集团未开通的店铺列表
     *
     * @param appver         the appver
     * @param shopSearchInfo the shop search info
     * @return the open organ list
     */
    @RequestMapping(value = "/is/{appver}/admin/business/notopen/shop/list", method = RequestMethod.POST)
    public ResponseEntity<Object> getNotOpenShopList(@PathVariable("appver") String appver, @RequestBody ShopSearchInfo shopSearchInfo) {
        LOGGER_OPENSHOPCONTROLLER.info("get Open Shop List start >>>>>> ");
        // 参数check
        if (null == shopSearchInfo.getStart() || null == shopSearchInfo.getPages()) {
            throw new ParamErrorException("");
        }

        Map<String, Object> data = adminManagementService.findNotOpenShop(shopSearchInfo);
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(data);
        LOGGER_OPENSHOPCONTROLLER.info(" <<<<<< get Open Shop List end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Do check response entity.
     * 审核店铺证照
     *
     * @param appver    the appver
     * @param checkInfo the check info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/admin/business/shop/license/check", method = RequestMethod.POST)
    public ResponseEntity<Object> doCheckLicence(@PathVariable("appver") String appver, @RequestBody ShopLicenseCheckInfo checkInfo) {
        LOGGER_OPENSHOPCONTROLLER.info("shop license check start >>>>>> ");
        if (null == checkInfo.getApplyId()
                || null == checkInfo.getLicenceId()
                || null == checkInfo.getSyscodeid()
                || null == checkInfo.getMatchid()
                || null == checkInfo.getCheckStatus()) {
            throw new ParamErrorException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(openShopBiz.checkLicence(checkInfo));
        resultInfo.setData(null);
        LOGGER_OPENSHOPCONTROLLER.info(" <<<<<< shop license check end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Gets license history.
     * 查询店铺证照历史
     *
     * @param appver     the appver
     * @param searchInfo the search info
     * @return the license history
     */
    @RequestMapping(value = "/is/{appver}/admin/business/shop/license/history", method = RequestMethod.POST)
    public ResponseEntity<Object> getLicenseHistory(@PathVariable("appver") String appver, @RequestBody LicenceSearchInfo searchInfo) {
        LOGGER_OPENSHOPCONTROLLER.info("get license history start >>>>>> ");
        if (null == searchInfo.getShopId()) {
            throw new ParamErrorException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(openShopBiz.getLicenseHistory(searchInfo));
        LOGGER_OPENSHOPCONTROLLER.info(" <<<<<< get license history end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

}
